package com.cljz.makepassenger.ui.adapter;

import android.content.Context;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cljz.makepassenger.R;
import com.cljz.makepassenger.model.net.bean.UserPointBean;

import java.util.ArrayList;
import java.util.List;

public class PointAdapter extends RecyclerView.Adapter<PointAdapter.Holder> {

    private final LayoutInflater inflater;
    private ArrayList<UserPointBean.DataBean.PointsListBean> data;
    private Context context;

    public PointAdapter(Context context) {
        data = new ArrayList<>();
        this.context = context;
        inflater = LayoutInflater.from(context);
    }

    public void addData(List<UserPointBean.DataBean.PointsListBean> bean,boolean isRefresh) {
        if (isRefresh){
            data.clear();
        }
        data.addAll(bean);
        notifyDataSetChanged();
    }

    @Override
    public Holder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(R.layout.item_transaction, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(Holder holder, int position) {
        UserPointBean.DataBean.PointsListBean bean = data.get(position);

        holder.name.setText(bean.getContent());
        holder.date.setText(bean.getAdd_time());

        if ("0".equals(bean.getIn_out())) {
            holder.change.setTextColor(Color.parseColor("#333333"));
            holder.change.setText("+ " + bean.getUser_points());
        } else {
            holder.change.setTextColor(Color.parseColor("#509ff8"));
            holder.change.setText("- " + bean.getUser_points());
        }
        holder.error.setVisibility(View.GONE);
    }

    @Override
    public int getItemCount() {
        return data == null ? 0 : data.size();
    }

    class Holder extends RecyclerView.ViewHolder {

        private final TextView name;
        private final TextView change;
        private final TextView date;
        private final TextView error;

        public Holder(View itemView) {
            super(itemView);
            name = (TextView) itemView.findViewById(R.id.tv_name);
            date = (TextView) itemView.findViewById(R.id.tv_date);
            change = (TextView) itemView.findViewById(R.id.tv_change);
            error = (TextView) itemView.findViewById(R.id.tv_withdraw_error_des);
        }
    }
}
